package com.naizhuai.ui.mianbusiness.action;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.naizhuai.R;
import com.naizhuai.ui.BaseActivity;
import com.naizhuai.ui.adapter.business.ActionAddImageAdapterHolder;
import com.naizhuai.ui.entity.SimpleKeyValueBean;
import com.naizhuai.ui.entity.mainbusiness.ActionSelectedProcessImageDetails;
import com.naizhuai.util.DialogUtils;
import com.naizhuai.util.ImageUtils;
import com.naizhuai.util.RxBus;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;

/**
 * 查看选中的照片详情的页面（删除，滑动查看操作）
 */
public class ActionShowChoseImageActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "ActionShowChoseImageActivity";
    public static final String IMAGE_DATA_DETAILS = "image_data_show_details";
    @BindView(R.id.image_details)
    ViewPager imagePager;
    @BindView(R.id.back_icon)
    ImageView backBtn;
    @BindView(R.id.image_details_number)
    TextView imageNumberTxt;
    @BindView(R.id.delete_image)
    ImageView deleteDetails;
    //第一个[0]为点击的第几个，从后面开始为传递的String[]
    private List<SimpleKeyValueBean> imagePathStrs;
    private int index;
    private String imageNumber;
    private ImageDetailsPagerAdapter targetAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initBaseData();
//        initVirtualData();
        initBaseView();
        initListener();
    }

    private void initVirtualData() {
        index = 0;
        imagePathStrs = new ArrayList<>();
//        imagePathStrs.add("a");
//        imagePathStrs.add("a");
//        imagePathStrs.add("a");
//        imagePathStrs.add("a");

    }


    private void initBaseData() {
        Intent intent = super.getIntent();
        ActionSelectedProcessImageDetails details = (ActionSelectedProcessImageDetails) intent.getSerializableExtra(IMAGE_DATA_DETAILS);
        index = details.getPosition();
        imagePathStrs = new ArrayList<>();
        imagePathStrs.addAll(details.getImageList());
        if(!details.isEdit())
            deleteDetails.setVisibility(View.GONE);
        else
            deleteDetails.setVisibility(View.VISIBLE);
    }

    private void initBaseView() {
        imageNumber = getText(R.string.image_number).toString();
        String text = String.format(imageNumber, index + 1, imagePathStrs.size());
        imageNumberTxt.setText(text);
        targetAdapter = new ImageDetailsPagerAdapter(this, imagePathStrs);
        imagePager.setAdapter(targetAdapter);
        imagePager.setCurrentItem(index);
    }

    private void initListener() {
        imagePager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                String text = String.format(imageNumber, position + 1, imagePathStrs.size());
                imageNumberTxt.setText(text);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        backBtn.setOnClickListener(this);
        deleteDetails.setOnClickListener(this);
    }


    @Override
    public int getLayoutId() {
        return R.layout.activity_show_image_details_layout;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_icon:
                super.onBackPressed();
                break;
            case R.id.delete_image:
                deleteImagePager();
                break;
        }

    }

    private void deleteImagePager() {
        DialogUtils.showSureDialog(this, getText(R.string.is_delete_image).toString(), new View.OnClickListener() {
            @Override
            public void onClick(View dialog) {
                int deleteIndex = imagePager.getCurrentItem();
                imagePathStrs.remove(deleteIndex);
                RxBus.getDefault().postSticky(deleteIndex);
                if (imagePathStrs.size() == 0) {
                    ActionShowChoseImageActivity.super.onBackPressed();
                } else {
                    targetAdapter.notifyDataSetChanged();
                    String text = String.format(imageNumber, imagePager.getCurrentItem() + 1, imagePathStrs.size());
                    imageNumberTxt.setText(text);
                }
            }
        });
    }

    //轮播图适配器
    private class ImageDetailsPagerAdapter extends PagerAdapter {
        private Context mContext;
        private List<SimpleKeyValueBean> imagePaths;

        public ImageDetailsPagerAdapter(Context context, List<SimpleKeyValueBean> imagePaths) {
            this.mContext = context;
            this.imagePaths = imagePaths;
        }

        @Override
        public int getCount() {
            return imagePaths.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView item = new ImageView(mContext);
            SimpleKeyValueBean simpleValue = imagePaths.get(position);
            String filePath = imagePaths.get(position).getValue();
            if (simpleValue.getKey() == ActionAddImageAdapterHolder.LOCAL_FILE_IMAGE) {
                File file = new File(filePath);
                ImageUtils.glideImageViewByFile(mContext,item,file);
            } else if (simpleValue.getKey() == ActionAddImageAdapterHolder.URL_NET_IMAGE) {
                ImageUtils.glideImageViewByUrl(mContext,item,filePath);
            }
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            item.setLayoutParams(params);
            container.addView(item);
            return item;
        }

        @Override
        public void destroyItem(ViewGroup collection, int position, Object view) {
            collection.removeView((View) view);
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }
    }


}
